﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace BillingManagment
{
    public partial class DailyPaid : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["connection"].ToString());
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFarmerList();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                Total();
                string temp = "0";
                SqlCommand cmd = new SqlCommand("Insert into tbl_DailyBhugtan(Farmer_ID,Cash, Fraid,Advance,Hydro,Other,Total,Date)values('" + ddlFarmer.SelectedValue + "','" + txtCash.Text + "','" + txtFraid.Text + "','" + txtAdvance.Text + "','" + txtHydro.Text + "','" + txtOther.Text + "','" + txtTotal.Text + "','" + txtStartDate.Text + "')", con);
                DataTable dt = new DataTable();
                SqlDataAdapter da1 = new SqlDataAdapter(cmd);
                da1.Fill(dt);
                SqlCommand cmd1 = new SqlCommand("select Advance_Rs from tbl_Saving_Ac where Farmer_ID='" + ddlFarmer.SelectedValue + "'", con);
                DataTable dt1 = new DataTable();
                SqlDataAdapter oAdp1 = new SqlDataAdapter(cmd1);
                oAdp1.Fill(dt1);
                if (dt1.Rows.Count > 0)
                {
                    temp = dt1.Rows[0][0].ToString();
                    temp = Convert.ToString(Convert.ToInt32(temp) - Convert.ToInt32(txtAdvance.Text));
                }
                SqlCommand cmd2 = new SqlCommand("Update tbl_Saving_Ac set Advance_Balance='" + temp + "'", con);
                DataTable dt2 = new DataTable();
                SqlDataAdapter oAdp2 = new SqlDataAdapter(cmd2);
                oAdp2.Fill(dt2);

                int TempTotal = 0;
                int TempTotal1 = 0;
                SqlCommand cmd3 = new SqlCommand("Select Balance from tbl_Current_Ac where Farmer_Id='" + ddlFarmer.SelectedValue + "' and ModifiedDate='" + txtStartDate.Text + "'", con);
                DataTable dt3 = new DataTable();
                SqlDataAdapter oAdp3 = new SqlDataAdapter(cmd3);
                oAdp3.Fill(dt3);

                if (dt3.Rows.Count > 0)
                {
                    TempTotal = Convert.ToInt32(dt3.Rows[0][0].ToString());
                    TempTotal1 = TempTotal - Convert.ToInt32(txtTotal.Text);
                }

                SqlCommand cmd4 = new SqlCommand("Update tbl_Current_Ac set PreviousAmount='"+ Convert.ToString(TempTotal)+"', PayedAmount='" + txtTotal.Text + "', Balance='" + Convert.ToString(TempTotal1) + "' where Farmer_Id='" + ddlFarmer.SelectedValue + "' and ModifiedDate='" + txtStartDate.Text + "'", con);
                DataTable dt4 = new DataTable();
                SqlDataAdapter oAdp4 = new SqlDataAdapter(cmd4);
                oAdp4.Fill(dt4);
                clearControl();
                Page.RegisterStartupScript("alert", "<script>alert('Data Enter Successfully.');</script>");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }       
        }

        private void Total()
        {
            txtTotal.Text = Convert.ToString(int.Parse(txtCash.Text) + int.Parse(txtFraid.Text) + int.Parse(txtHydro.Text) + int.Parse(txtAdvance.Text) + int.Parse(txtOther.Text));
        }

        public void BindFarmerList()
        {
            SqlCommand cmd = new SqlCommand("select ID, (Name +' '+ village) as FName from tbl_Farmer", con);
            DataTable dt = new DataTable();
            SqlDataAdapter oAdp = new SqlDataAdapter(cmd);
            oAdp.Fill(dt);
            ddlFarmer.DataSource = dt;
            ddlFarmer.DataValueField = "ID";
            ddlFarmer.DataTextField = "FName";
            ddlFarmer.DataBind();
            ddlFarmer.Items.Insert(0, "<----Select---->");
        }

        protected void txtCash_TextChanged(object sender, EventArgs e)
        {
            if (txtCash.Text == "" || txtFraid.Text == "" || txtHydro.Text == "" || txtOther.Text == "" || txtAdvance.Text == "")
            {
                txtCash.Text = "0";
                txtFraid.Text = "0";
                txtHydro.Text = "0";
                txtAdvance.Text = "0";
                txtOther.Text = "0";
            }
            else
            {
                txtTotal.Text = Convert.ToString(Convert.ToInt32(txtCash.Text) + Convert.ToInt32(txtFraid.Text) + Convert.ToInt32(txtHydro.Text) + Convert.ToInt32(txtAdvance.Text) + Convert.ToInt32(txtOther.Text));
            }
        }

        protected void txtFraid_TextChanged(object sender, EventArgs e)
        {
            if (txtCash.Text == "" || txtFraid.Text == "" || txtHydro.Text == "" || txtOther.Text == "" || txtAdvance.Text == "")
            {
                txtCash.Text = "0";
                txtFraid.Text = "0";
                txtHydro.Text = "0";
                txtAdvance.Text = "0";
                txtOther.Text = "0";
            }
            else
            {
                txtTotal.Text = Convert.ToString(Convert.ToInt32(txtCash.Text) + Convert.ToInt32(txtFraid.Text) + Convert.ToInt32(txtHydro.Text) + Convert.ToInt32(txtAdvance.Text) + Convert.ToInt32(txtOther.Text));
            }
        }

        protected void txtAdvance_TextChanged(object sender, EventArgs e)
        {
            if (txtCash.Text == "" || txtFraid.Text == "" || txtHydro.Text == "" || txtOther.Text == "" || txtAdvance.Text == "")
            {
                txtCash.Text = "0";
                txtFraid.Text = "0";
                txtHydro.Text = "0";
                txtAdvance.Text = "0";
                txtOther.Text = "0";
            }
            else
            {
                txtTotal.Text = Convert.ToString(Convert.ToInt32(txtCash.Text) + Convert.ToInt32(txtFraid.Text) + Convert.ToInt32(txtHydro.Text) + Convert.ToInt32(txtAdvance.Text) + Convert.ToInt32(txtOther.Text));
            }
        }

        protected void txtHydro_TextChanged(object sender, EventArgs e)
        {
            if (txtCash.Text == "" || txtFraid.Text == "" || txtHydro.Text == "" || txtOther.Text == "" || txtAdvance.Text == "")
            {
                txtCash.Text = "0";
                txtFraid.Text = "0";
                txtHydro.Text = "0";
                txtAdvance.Text = "0";
                txtOther.Text = "0";
            }
            else
            {
                txtTotal.Text = Convert.ToString(Convert.ToInt32(txtCash.Text) + Convert.ToInt32(txtFraid.Text) + Convert.ToInt32(txtHydro.Text) + Convert.ToInt32(txtAdvance.Text) + Convert.ToInt32(txtOther.Text));
            }
        }

        protected void txtOther_TextChanged(object sender, EventArgs e)
        {
            if (txtCash.Text == "" || txtFraid.Text == "" || txtHydro.Text == "" || txtOther.Text == "" || txtAdvance.Text == "")
            {
                txtCash.Text = "0";
                txtFraid.Text = "0";
                txtHydro.Text = "0";
                txtAdvance.Text = "0";
                txtOther.Text = "0";
            }
            else
            {
                txtTotal.Text = Convert.ToString(Convert.ToInt32(txtCash.Text) + Convert.ToInt32(txtFraid.Text) + Convert.ToInt32(txtHydro.Text) + Convert.ToInt32(txtAdvance.Text) + Convert.ToInt32(txtOther.Text));
            }
        }

        public void clearControl()
        {
            txtAdvance.Text = "";
            txtCash.Text = "";
            txtFraid.Text = "";
            txtHydro.Text = "";
            txtOther.Text = "";
            txtTotal.Text = "";
        }

    }
}